﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace BillingManagment
{
    public partial class SellingSlip_Old : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            SqlCommand cmd = new SqlCommand("select * from Temp_Items", con);
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = ds;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
            }
            else
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            con.Close();
        }


        protected void AddNewCustomer(object sender, EventArgs e)
        {
            string Product = ((Label)GridView1.FooterRow.FindControl("lblProduct")).Text;
            string Bucket = ((TextBox)GridView1.FooterRow.FindControl("txtBucket")).Text;
            string Rate = ((TextBox)GridView1.FooterRow.FindControl("txtRate")).Text;
            string Wait = ((TextBox)GridView1.FooterRow.FindControl("txtWait")).Text;
            //string temp = Convert.ToString (((Convert.ToInt32(Rate) * 2.5) / 100) * Convert.ToInt32(Wait));
            string Amount = ((TextBox)GridView1.FooterRow.FindControl("txtAmount")).Text;
            //SqlConnection con = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand("insert into Temp_Items(Product_Name, NoOfBucket,Rate_Per_Forty,Product_Wait,Total_Amount) values ('" + Product + "','" + Bucket + "','" + Rate + "','" + Wait + "','" + Amount + "')", con);
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        protected void EditCustomer(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }
        protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }
        protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
        {
            string Product = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblProduct")).Text;
            string Bucket = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBucket")).Text;
            string Rate = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtRate")).Text;
            string Wait = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtWait")).Text;
            string Amount = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAmount")).Text;
            SqlCommand cmd = new SqlCommand("update Temp_Items set Product_Name='" +Product+"',NoOfBucket='"+Bucket+"',Rate_Per_Forty='"+Rate+"',Product_Wait='"+Wait+"',Total_Amount='"+Amount+"'",con);
            GridView1.EditIndex = -1;
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void DeleteCustomer(object sender, EventArgs e)
        {
            LinkButton lnkRemove = (LinkButton)sender;           
            //SqlCommand cmd = new SqlCommand("delete from Temp_Items where S_ID= '"++"'",con);
            //SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            //da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
}